//
//  SISSCommonVerticalShowImageViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/12/24.
//  Copyright © 2018 优房网. All rights reserved.
//

#import "SISSCommonVerticalShowImageViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SISSPhotoBrowser.h"

static NSString *verticalShowImageCollectViewCell = @"verticalShowImageCollectViewCell";
@interface SISSCommonVerticalShowImageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,MWPhotoBrowserDelegate>

// collectionView
@property (weak, nonatomic) UICollectionView *collectionView;
// flowLayout
@property (weak, nonatomic) UICollectionViewFlowLayout *flowLayout;

@end

@implementation SISSCommonVerticalShowImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self requestContractImageData];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    // 一定要在viewDidAppear里面设置itemSize;只有在该方法里面才能拿到真是的frame
    self.flowLayout.itemSize = self.collectionView.bounds.size;
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.urlStrArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:verticalShowImageCollectViewCell forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.urlStrArr[indexPath.item]] placeholderImage:[UIImage imageNamed:@""]];
    [cell.contentView addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(cell.contentView).offset(4);
        make.bottom.right.equalTo(cell.contentView).offset(-4);
    }];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MWPhotoBrowser *photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    [photoBrowser setCurrentPhotoIndex:indexPath.item];
    //    [photoBrowser showNextPhotoAnimated:YES];
    //    [photoBrowser showPreviousPhotoAnimated:YES];
    SISSNavigationController *navigationController = [[SISSNavigationController alloc] initWithRootViewController:photoBrowser];
    navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:navigationController animated:YES completion:nil];
}

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.urlStrArr.count;
}

- (id<MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    
    //创建图片模型
    MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:self.urlStrArr[index]]];
    return photo;
}

- (void)requestContractImageData {
    
}

- (void)reloadData {
    [self.collectionView reloadData];
}

/**
 确定按钮点击事件
 */
- (void)uploadButtonClickAction {
    
}


- (void)setupSubviews {
    [super setupSubviews];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 1;
    flowLayout.minimumInteritemSpacing = 2;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    self.flowLayout = flowLayout;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.pagingEnabled = YES;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:verticalShowImageCollectViewCell];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
    UIButton *uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [uploadButton setTitle:@"上传" forState:UIControlStateNormal];
    [uploadButton setBackgroundColor:MAIN_COLOR];
    [uploadButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [uploadButton addTarget:self action:@selector(uploadButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    uploadButton.hidden = self.isHistory;
    [self.view addSubview:uploadButton];
    
    
    WeakSelf
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_topLayoutGuideBottom);
        make.left.right.equalTo(weakSelf.view);
    }];
    
    [uploadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        if (self.isHistory) {
            make.height.mas_equalTo(0.01);
        } else {
            make.height.mas_equalTo(44);
        }
        make.top.equalTo(collectionView.mas_bottom);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(weakSelf.view);
        }
    }];
}


@end
